<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="被考核员工ID" prop="userId">
                    <el-input
                        v-model="queryParams.userId"
                        placeholder="请输入被考核员工ID"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="考核周期" prop="appraisalCycle">
                    <el-input
                        v-model="queryParams.appraisalCycle"
                        placeholder="请输入考核周期"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-opportunity"
          size="mini"
          @click="handleGenerate"
          v-hasPermi="['system:appraisal:generate']"
        >生成绩效考核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:appraisal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appraisalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="考核ID" align="center" prop="appraisalId" />
              <el-table-column label="被考核员工ID" align="center" prop="userId" />
              <el-table-column label="考核周期 " align="center" prop="appraisalCycle" />
              <el-table-column label="接单数" align="center" prop="receivingOrdersNum" />
              <el-table-column label="接单率" align="center" prop="receivingOrdersRatio">
                <template slot-scope="scope">
                  {{ isNaN(parseFloat(scope.row.receivingOrdersRatio)) ? '' : (parseFloat(scope.row.receivingOrdersRatio) * 100).toFixed(2) + '%' }}
                </template>
              </el-table-column>
              <el-table-column label="接单总金额" align="center" prop="receivingOrdersAmount" />
              <el-table-column label="黄单率" align="center" prop="failureRatio">
                <template slot-scope="scope">
                  {{ isNaN(parseFloat(scope.row.failureRatio)) ? '' : (parseFloat(scope.row.failureRatio) * 100).toFixed(2) + '%' }}
                </template>
              </el-table-column>
              <el-table-column label="黄单数" align="center" prop="failureNum" />
              <el-table-column label="白单数" align="center" prop="whiteNum" />
              <el-table-column label="包单率" align="center" prop="externalTechnologyRatio">
                <template slot-scope="scope">
                  {{ isNaN(parseFloat(scope.row.externalTechnologyRatio)) ? '' : (parseFloat(scope.row.externalTechnologyRatio) * 100).toFixed(2) + '%' }}
                </template>
              </el-table-column>
              <el-table-column label="拨单差价" align="center" prop="externalDif" />
              <el-table-column label="拨单金额" align="center" prop="externalAmount" />
              <el-table-column label="成交率" align="center" prop="dealRatio">
                <template slot-scope="scope">
                  {{ isNaN(parseFloat(scope.row.dealRatio)) ? '' : (parseFloat(scope.row.dealRatio) * 100).toFixed(2) + '%' }}
                </template>
              </el-table-column>
              <el-table-column label="成交数" align="center" prop="dealNum" />
              <el-table-column label="成交金额" align="center" prop="dealAmount" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:appraisal:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:appraisal:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />


    <!-- 生成绩效对话框 -->
    <el-dialog :title="generateTitle" :visible.sync="generateOpen" width="480px" append-to-body>
      <el-form ref="generateForm" :model="generateForm" :rules="generateRules" label-width="100px">
        <el-form-item label="选择考核周期" prop="dateRange">
          <el-date-picker
            v-model="generateForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            style="width: 100%;">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitGenerate">确 定</el-button>
        <el-button @click="cancelGenerate">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listAppraisal, getAppraisal, delAppraisal, addAppraisal, updateAppraisal, generateAppraisal } from "@/api/system/appraisal"

  export default {
    name: "Appraisal",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 绩效考核结果表格数据
              appraisalList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示生成绩效弹出层
        generateOpen: false,
        // 生成绩效弹出层标题
        generateTitle: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                        userId: null,
                        appraisalCycle: null,
                        finalScore: null,
                        finalGrade: null,
                        comments: null,
                        status: null,
                        receivingOrdersNum: null,
                        receivingOrdersRatio: null,
                        receivingOrdersAmount: null,
                        failureRatio: null,
                        failureNum: null,
                        whiteNum: null,
                        externalTechnologyRatio: null,
                        externalDif: null,
                        externalAmount: null,
                        dealRatio: null,
                        dealNum: null,
                        dealAmount: null,
        },
        // 表单参数
        form: {
                        appraisalId: null,
                        userId: null,
                        appraisalCycle: null,
                        finalScore: null,
                        finalGrade: null,
                        comments: null,
                        status: null,
                        receivingOrdersNum: null,
                        receivingOrdersRatio: null,
                        receivingOrdersAmount: null,
                        failureRatio: null,
                        failureNum: null,
                        whiteNum: null,
                        externalTechnologyRatio: null,
                        externalDif: null,
                        externalAmount: null,
                        dealRatio: null,
                        dealNum: null,
                        dealAmount: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null
        },
        // 生成绩效表单
        generateForm: {},
        // 表单校验
        rules: {
                        userId: [
                    { required: true, message: "被考核员工ID不能为空", trigger: "blur" }
                  ],
                        appraisalCycle: [
                    { required: true, message: "考核周期 不能为空", trigger: "blur" }
                  ],
                        status: [
                    { required: true, message: "状态不能为空", trigger: "change" }
                  ],
        },
        // 生成绩效表单校验
        generateRules: {
          dateRange: [
            { required: true, message: "考核周期不能为空", trigger: "change" }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询绩效考核结果列表 */
      getList() {
        this.loading = true
        listAppraisal(this.queryParams).then(response => {
          this.appraisalList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 生成绩效-取消按钮
      cancelGenerate() {
        this.generateOpen = false;
        this.resetGenerateForm();
      },
      // 表单重置
      reset() {
        this.form = {
                        appraisalId: null,
                        userId: null,
                        appraisalCycle: null,
                        finalScore: null,
                        finalGrade: null,
                        comments: null,
                        status: null,
                        receivingOrdersNum: null,
                        receivingOrdersRatio: null,
                        receivingOrdersAmount: null,
                        failureRatio: null,
                        failureNum: null,
                        whiteNum: null,
                        externalTechnologyRatio: null,
                        externalDif: null,
                        externalAmount: null,
                        dealRatio: null,
                        dealNum: null,
                        dealAmount: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null
        }
        this.resetForm("form")
      },
      // 生成绩效表单重置
      resetGenerateForm() {
        this.generateForm = {
          dateRange: []
        };
        this.resetForm("generateForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.appraisalId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加绩效考核结果"
      },
      /** 生成绩效考核按钮操作 */
      handleGenerate() {
        this.resetGenerateForm();
        this.generateOpen = true;
        this.generateTitle = "生成绩效考核";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const appraisalId = row.appraisalId || this.ids
        getAppraisal(appraisalId).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改绩效考核结果"
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.appraisalId != null) {
              updateAppraisal(this.form).then(response => {
                this.$modal.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addAppraisal(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 生成绩效提交按钮 */
      submitGenerate() {
        this.$refs["generateForm"].validate(valid => {
          if (valid) {
            const params = {
              beginTime: this.generateForm.dateRange[0],
              endTime: this.generateForm.dateRange[1]
            };
            this.loading = true;
            generateAppraisal(params).then(response => {
              this.$modal.msgSuccess("生成任务已成功触发，请稍后刷新列表");
              this.generateOpen = false;
              this.getList();
            }).finally(() => {
              this.loading = false;
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const appraisalIds = row.appraisalId || this.ids
        this.$modal.confirm('是否确认删除绩效考核结果编号为"' + appraisalIds + '"的数据项？').then(function() {
          return delAppraisal(appraisalIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('system/appraisal/export', {
      ...this.queryParams
    }, `appraisal_${new Date().getTime()}.xlsx`)
  }
  }
  }
</script>
